package cn.new7as.cas5s.ui.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.hjq.http.EasyHttp;
import com.hjq.http.lifecycle.ActivityLifecycle;
import com.hjq.http.listener.OnHttpListener;

import java.util.List;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.ui.repay.RepaysActivity;
import cn.new7as.cas5s.ui.slideshow.MainActivity;
import cn.new7as.cas5s.databinding.FragmentHomeBinding;
import cn.new7as.cas5s.tool.util.SharePreferenceUtils;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.easy.netapi.ProductCateApi;
import cn.new7as.cas5s.ui.repay.OrdersDetailActivity;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        binding.mothing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).open();
            }
        });

        return root;
    }


    @Override
    public void onResume() {
        super.onResume();
        EasyHttp.get(new ActivityLifecycle(getActivity()))
                .api(new ProductCateApi(new SharePreferenceUtils(getActivity()).getString("useruniquecode", "")))
                .request(new OnHttpListener<HttpData<List<ProductCateApi.ProductCateData>>>() {
                    @SuppressLint("SuspiciousIndentation")
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<List<ProductCateApi.ProductCateData>> result) {
                        if (result.getResult() == null) return;

                        if (result.getResult().size() > 1) {
                            Gridadapter gridadapter = new Gridadapter(result.getResult().subList(1, result.getResult().size()), (MainActivity) getActivity());
                            binding.maingride.setAdapter(gridadapter);
                            binding.maingride.setOnItemClickListener((parent, view, position, id) -> {

                            });
                        }
                        if (!result.getResult().isEmpty()) {
                            binding.topmoubt.setText(result.getResult().get(0).amountRange.replace("₹", ""));
                            switch (result.getResult().get(0).loanStatus) {
                                case 0:
                                    binding.topget.setText("Get Started");
                                    break;
                                case 1:
                                    binding.topget.setText("Pending");
                                    break;
                                case 2:
                                    binding.topget.setText("Repay");
                                    break;
                                case 3:
                                    binding.topget.setText("Overdue");
                                    binding.topget.setBackgroundResource(R.drawable.overdueo);
                                    break;
                                case 4:
                                    binding.topget.setText("Payment Failed");
                                    break;


                            }
                            binding.topget.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    switch (result.getResult().get(0).loanStatus) {
                                        case 0:
                                            ((MainActivity) getActivity()).status(result.getResult().get(0).productCategorySubCode);
                                            break;
                                        case 1:

                                            godeta(result.getResult().get(0).productCategorySubCode, false);
                                            break;
                                        case 2:

                                            godeta(result.getResult().get(0).productCategorySubCode, true);
                                            break;
                                        case 3:

                                            godeta(result.getResult().get(0).productCategorySubCode, true);
                                            break;
                                        case 4:

                                            godeta(result.getResult().get(0).productCategorySubCode, false);

                                            break;

                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        ((MainActivity) getActivity()).deal(throwable);
                    }
                });
    }

    void godeta(String pcode, boolean aa) {
        OrderApi.Data data = new OrderApi().new Data();
        Intent intent;
        data.fo = 0;
        if (aa) {
            intent = new Intent(getActivity(), RepaysActivity.class);

        } else
            intent = new Intent(getActivity(), OrdersDetailActivity.class);

        Bundle bundle = new Bundle();

        data.productCategorySubCode = pcode;
        bundle.putSerializable("ddba", data);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}